home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 22 / AACD 22.iso / AACD / Online / Apache / lib / php / PEAR / Installer.php
Encoding:
PHP Script  |  2001-03-06  |  11.2 KB  |  465 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group                   |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@fast.no>                                   |
  17. // |                                                                      |
  18. // +----------------------------------------------------------------------+
  19. //
  20.  
  21. require_once "PEAR.php";
  22.  
  23. /**
  24.  * Administration class used to install PEAR packages and maintain the
  25.  * class definition cache.
  26.  *
  27.  * @since PHP 4.0.2
  28.  * @author Stig Bakken <ssb@fast.no>
  29.  */
  30. class PEAR_Installer extends PEAR {
  31.  
  32.     // {{{ properties
  33.  
  34.     /** stack of elements, gives some sort of XML context */
  35.     var $element_stack;
  36.  
  37.     /** name of currently parsed XML element */
  38.     var $current_element;
  39.  
  40.     /** assoc with information about the package */
  41.     var $pkginfo = array();
  42.  
  43.     /** name of the package directory, for example Foo-1.0 */
  44.     var $pkgdir;
  45.  
  46.     /** directory where PHP code files go */
  47.     var $pear_phpdir = PEAR_INSTALL_DIR;
  48.  
  49.     /** directory where PHP extension files go */
  50.     var $pear_extdir = PEAR_EXTENSION_DIR;
  51.  
  52.     /** directory where documentation goes */
  53.     var $pear_docdir = '';
  54.  
  55.     /** directory where the package wants to put files, relative
  56.      *  to one of the three previous dirs
  57.      */
  58.     var $destdir = '';
  59.  
  60.     /** debug mode (boolean) */
  61.     var $debug = false;
  62.  
  63.     /** class loading cache */
  64.     var $cache = array();
  65.  
  66.     /** temporary directory */
  67.     var $tmpdir;
  68.  
  69.     /** file pointer for cache file if open */
  70.     var $cache_fp;
  71.  
  72.     // }}}
  73.  
  74.     // {{{ constructor
  75.  
  76.     function PEAR_Installer() {
  77.     $this->PEAR();
  78.     $this->cacheLoad("$this->pear_phpdir/.cache");
  79.     }
  80.  
  81.     // }}}
  82.     // {{{ destructor
  83.  
  84.     function _PEAR_Installer() {
  85.     $this->_PEAR();
  86.     if ($this->tmpdir && is_dir($this->tmpdir)) {
  87.         system("rm -rf $this->tmpdir");
  88.     }
  89.     if ($this->cache_fp && is_resource($this->cache_fp)) {
  90.         flock($this->cache_fp, LOCK_UN);
  91.         fclose($this->cache_fp);
  92.     }
  93.     $this->tmpdir = null;
  94.     $this->cache_fp = null;
  95.     }
  96.  
  97.     // }}}
  98.  
  99.     // {{{ cacheLock()
  100.  
  101.     function cacheLock() {
  102.     $fp = $this->cache_fp;
  103.     if (!is_resource($fp)) {
  104.         $this->cache_fp = $fp = fopen($this->cache_file, "r");
  105.     }
  106.     return flock($fp, LOCK_EX);
  107.     }
  108.  
  109.     // }}}
  110.     // {{{ cacheUnlock()
  111.  
  112.     function cacheUnlock() {
  113.     $fp = $this->cache_fp;
  114.     if (!is_resource($fp)) {
  115.         $this->cache_fp = $fp = fopen($this->cache_file, "r");
  116.         $doclose = true;
  117.     }
  118.     $ret = flock($fp, LOCK_EX);
  119.     if ($doclose) {
  120.         fclose($fp);
  121.     }
  122.     return $ret;
  123.     }
  124.  
  125.     // }}}
  126.     // {{{ cacheLoad()
  127.  
  128.     function cacheLoad($file) {
  129.     $this->cache_file = $file;
  130.     if (!file_exists($file)) {
  131.         touch($file);
  132.     }
  133.     $fp = $this->cache_fp = fopen($file, "r");
  134.     $this->cacheLock();
  135.     while ($line = fgets($fp, 2048)) {
  136.         list($type, $name, $file) = explode(" ", trim($line));
  137.         $this->cache[$type][$name] = $file;
  138.     }
  139.     }
  140.  
  141.     // }}}
  142.     // {{{ cacheSave()
  143.  
  144.     function cacheSave() {
  145.     $fp = $this->cache_fp;
  146.     $wfp = fopen($this->cache_file, "w");
  147.     if (!$wfp) {
  148.         return false;
  149.     }
  150.     if (is_resource($fp)) {
  151.         fclose($fp);
  152.     }
  153.     $this->cache_fp = $fp = $wfp;
  154.     reset($this->cache);
  155.     while (list($type, $entry) = each($this->cache)) {
  156.         reset($entry);
  157.         while (list($name, $file) = each($entry)) {
  158.         fwrite($fp, "$type $name $file\n");
  159.         }
  160.     }
  161.     fclose($fp);
  162.     $this->cache_fp = $fp = null;
  163.     }
  164.  
  165.     // }}}
  166.     // {{{ cacheUpdateFrom()
  167.  
  168.     function cacheUpdateFrom($file) {
  169.     $new = $this->classesDeclaredBy($file);
  170.     reset($new);
  171.     while (list($i, $name) = each($new)) {
  172.         $this->cache['class'][$name] = $file;
  173.     }
  174.     }
  175.  
  176.     // }}}
  177.  
  178.     // {{{ install()
  179.  
  180.     /**
  181.      * Installs the files within the package file specified.
  182.      *
  183.      * @param $pkgfile path to the package file
  184.      *
  185.      * @return bool true if successful, false if not
  186.      */
  187.     function install($pkgfile) {
  188.     if (!file_exists($pkgfile)) {
  189.         return new PEAR_Installer_Error("No such file: $pkgfile");
  190.     }
  191.  
  192.     $fp = popen("gzip -dc $pkgfile | tar -tf -", "r");
  193.     if (!$fp) {
  194.         return new PEAR_Installer_Error("Unable to examine $pkgfile (gzip or tar failed)\n");
  195.     }
  196.     while ($line = fgets($fp, 4096)) {
  197.         $line = rtrim($line);
  198.         if (preg_match('!^[^/]+/package.xml$!', $line)) {
  199.         if ($descfile) {
  200.             return new PEAR_Installer_Error("Invalid package: multiple package.xml files at depth one!\n");
  201.         }
  202.         $descfile = $line;
  203.         }
  204.     }
  205.     pclose($fp);
  206.  
  207.     if (!$descfile) {
  208.         return new PEAR_Installer_Error("Invalid package: no package.xml file found!\n");
  209.     }
  210.  
  211.     $this->tmpdir = tempnam("/tmp", "pear");
  212.     if (!mkdir($this->tmpdir, 0755)) {
  213.         return new PEAR_Installer_Error("Unable to create temporary directory $this->tmpdir.\n");
  214.     }
  215.  
  216.     $pwd = trim(`pwd`);
  217.  
  218.     if (substr($pkgfile, 0, 1) == "/") {
  219.         $pkgfilepath = $pkgfile;
  220.     } else {
  221.         $pkgfilepath = $pwd.'/'.$pkgfile;
  222.     }
  223.  
  224.     if (!chdir($this->tmpdir)) {
  225.         return new PEAR_Installer_Error("Unable to chdir to $this->tmpdir.\n");
  226.     }
  227.  
  228.     system("gzip -dc $pkgfilepath | tar -xf -");
  229.  
  230.     if (!file_exists($descfile)) {
  231.         return new PEAR_Installer_Error("Huh?  No package.xml file after extracting the archive.\n");
  232.     }
  233.  
  234.     $this->pkgdir = dirname($descfile);
  235.  
  236.     $fp = fopen($descfile, "r");
  237.     $xp = xml_parser_create();
  238.     if (!$xp) {
  239.         return new PEAR_Installer_Error("Unable to create XML parser.\n");
  240.     }
  241.     xml_set_object($xp, &$this);
  242.     xml_set_element_handler($xp, "start_handler", "end_handler");
  243.     xml_set_character_data_handler($xp, "char_handler");
  244.     xml_parser_set_option($xp, XML_OPTION_CASE_FOLDING, false);
  245.  
  246.     $this->element_stack = array();
  247.     $this->pkginfo = array();
  248.     $this->current_element = false;
  249.     $destdir = '';
  250.  
  251.     while ($data = fread($fp, 2048)) {
  252.         if (!xml_parse($xp, $data, feof($fp))) {
  253.         $err = new PEAR_Installer_Error(sprintf("XML error: %s at line %d",
  254.                             xml_error_string(xml_get_error_code($xp)),
  255.                             xml_get_current_line_number($xp)));
  256.         xml_parser_free($xp);
  257.         return $err;
  258.         }
  259.     }
  260.  
  261.     xml_parser_free($xp);
  262.  
  263.     return true;
  264.     }
  265.  
  266.     // }}}
  267.     // {{{ start_handler()
  268.  
  269.     function start_handler($xp, $name, $attribs) {
  270.     array_push($this->element_stack, $name);
  271.     $this->current_element = $name;
  272.     switch ($name) {
  273.         case "Package":
  274.         if (strtolower($attribs["Type"]) != "binary") {
  275.             return new PEAR_Installer_Error("Invalid package: only binary packages supported yet.\n");
  276.         }
  277.         $this->pkginfo['pkgtype'] = strtolower($attribs["Type"]);
  278.         break;
  279.     }
  280.     }
  281.  
  282.     // }}}
  283.     // {{{ end_handler()
  284.  
  285.     function end_handler($xp, $name) {
  286.     array_pop($this->element_stack);
  287.     $this->current_element = $this->element_stack[sizeof($this->element_stack)-1];
  288.     }
  289.  
  290.     // }}}
  291.     // {{{ char_handler()
  292.  
  293.     function char_handler($xp, $data) {
  294.     switch ($this->current_element) {
  295.         case "DestDir":
  296.         $this->destdir = trim($data);
  297.         if (substr($this->destdir, 0, 1) == "/") {
  298.             $this->destdir = substr($this->destdir, 1);
  299.         }
  300.         break;
  301.         case "Dir":
  302.         if (!$this->pear_phpdir) {
  303.             break;
  304.         }
  305.         $dir = trim($data);
  306.         $d = "$this->pear_phpdir/$this->destdir/$dir";
  307.         if (is_file($d)) {
  308.             print "Error: wanted to create the directory $d\n";
  309.             print "       but it was already a file.\n";
  310.             break;
  311.         }
  312.         if (is_dir($d)) {
  313.             break;
  314.         }
  315.         if (!mkdir($d, 0755)) {
  316.             print "Error: could not mkdir $d\n";
  317.             break;
  318.         }
  319.         if ($this->debug) print "[debug] created directory $d\n";
  320.         break;
  321.         case "File":
  322.         if (!$this->pear_phpdir) {
  323.             break;
  324.         }
  325.         $file = trim($data);
  326.         $d = "$this->pear_phpdir/$this->destdir";
  327.         if (!copy("$this->pkgdir/$file", "$d/$file")) {
  328.             print "Error: failed to copy $this->pkgdir/$file to $d\n";
  329.             break;
  330.         }
  331.         $this->cacheUpdateFrom("$d/$file");
  332.         if ($this->debug) print "[debug] installed $d/$file\n";
  333.         break;
  334.         case "ExtDir":
  335.         if (!$this->pear_extdir) {
  336.             break;
  337.         }
  338.         $dir = trim($data);
  339.         $d = "$this->pear_extdir/$this->destdir/$dir";
  340.         if (is_file($d)) {
  341.             print "Error: wanted to create the directory $d\n";
  342.             print "       but it was already a file.\n";
  343.             break;
  344.         }
  345.         if (is_dir($d)) {
  346.             continue 2;
  347.         }
  348.         if (!mkdir($d, 0755)) {
  349.             print "Error: could not mkdir $d\n";
  350.             break;
  351.         }
  352.         if ($this->debug) print "[debug] created directory $d\n";
  353.         break;
  354.         case "ExtFile":
  355.         if (!$this->pear_extdir) {
  356.             break;
  357.         }
  358.         $file = trim($data);
  359.         $d = "$this->pear_extdir/$this->destdir";
  360.         if (!copy("$this->pkgdir/$file", "$d/$file")) {
  361.             print "Error: failed to copy $this->pkgdir/$file to $d\n";
  362.             break;
  363.         }
  364.         if ($this->debug) print "[debug] installed $d/$file\n";
  365.         break;
  366.         case "DocDir":
  367.         if (!$this->pear_docdir) {
  368.             break;
  369.         }
  370.         $dir = trim($data);
  371.         $d = "$this->pear_docdir/$this->destdir/$dir";
  372.         if (is_file($d)) {
  373.             print "Error: wanted to create the directory $d\n";
  374.             print "       but it was already a file.\n";
  375.             break;
  376.         }
  377.         if (is_dir($d)) {
  378.             break;
  379.         }
  380.         if (!mkdir($d, 0755)) {
  381.             print "Error: could not mkdir $d\n";
  382.             break;
  383.         }
  384.         if ($this->debug) print "[debug] created directory $d\n";
  385.         break;
  386.         case "DocFile":
  387.         if (!$this->pear_docdir) {
  388.             break;
  389.         }
  390.         $file = trim($data);
  391.         $d = "$this->pear_docdir/$this->destdir";
  392.         if (!copy("$this->pkgdir/$file", "$d/$file")) {
  393.             print "Error: failed to copy $this->pkgdir/$file to $d\n";
  394.             break;
  395.         }
  396.         if ($this->debug) {
  397.             print "[debug] installed $d/$file\n";
  398.         }
  399.         break;
  400.     }
  401.     }
  402.  
  403.     // }}}
  404.  
  405.     // {{{ classesDeclaredBy()
  406.  
  407.     /**
  408.      * Find out which new classes are defined by a file.
  409.      *
  410.      * @param $file file name passed to "include"
  411.      *
  412.      * @return array classes that were defined
  413.      */
  414.     function classesDeclaredBy($file) {
  415.     $before = get_declared_classes();
  416.     include($file);
  417.     $after = get_declared_classes();
  418.     // using array_slice to renumber array
  419.     $diff = array_slice(array_diff($after, $before), 0);
  420.     return $diff;
  421.     }
  422.  
  423. // }}}
  424.  
  425.     // {{{ lockDir()
  426.  
  427.     /**
  428.      * Uses advisory locking (flock) to temporarily claim $dir as its
  429.      * own.
  430.      *
  431.      * @param $dir the directory to lock
  432.      *
  433.      * @return bool true if successful, false if not
  434.      */
  435.     function lockDir($dir) {
  436.     $lockfile = "$dir/.lock";
  437.     if (!file_exists($lockfile)) {
  438.         if (!touch($lockfile)) {
  439.         // could not create lockfile!
  440.         return false;
  441.         }
  442.     }
  443.     $fp = fopen($lockfile, "r");
  444.     if (!flock($fp, LOCK_EX)) {
  445.         // could not create lock!
  446.         return false;
  447.     }
  448.     return true;
  449.     }
  450.  
  451.     // }}}
  452. }
  453.  
  454. class PEAR_Installer_Error extends PEAR_Error {
  455.     // {{{ constructor
  456.  
  457.     function PEAR_Installer_Error($msg) {
  458.     $this->PEAR_Error($msg, 0, PEAR_ERROR_DIE);
  459.     }
  460.  
  461.     // }}}
  462. }
  463.  
  464. ?>
  465.